
import { Toast } from 'antd-mobile';
import { searchCustomerOrder, copyToCar, cancelOrder, getListOrder } from '../services/order';

export default {
  namespace: 'myorders',

  state: {
    ordersList: [],
    isError: false,
    searchData: {},
  },

  effects: {
    *searchCustomerOrder({ payload }, { call, put }) {
      const params = {
        customerId: 1,
        // pageNum: payload.pageNum,
        // distributionDateStart: '2018-05-25 00:00:00',
        orderNo: payload.orderNo,
      };
      console.log(222, params);
      const response = yield call(searchCustomerOrder, params);
      console.log(444, response);
      if (response.code === 0) {
        yield put({
          type: 'getSearchOrder',
          payload: response,
          tabData: payload.tabData,
        });
      } else {
        yield put({
          type: 'errorMsg',
          payload: {
            code: response.code,
            msg: response.msg,
          },
        });
      }
    },
    *copyToCar({ payload }, { call }) {
      const response = yield call(copyToCar, payload);
      console.log(2333, response);
      if (response.code === 0) {
        Toast.success('已经拷贝到购物车', 1);
      } else {
        Toast.fail('操作失败', 1);
      }
    },
    *cancelOrder({ payload }, { call, put }) {
      const response = yield call(cancelOrder, payload);
      if (response.code === 0) {
        Toast.success('取消成功', 1);
      }
      yield put({
        type: 'getListOrder',
        payload: {
          customerId: 1,
          pageNum: 0,
        },
      });
    },
    *getListOrder({ payload }, { call, put }) {
      const response = yield call(getListOrder, payload);
      if (response.code === 0) {
        yield put({
          type: 'getCustomerOrder',
          payload: response.data,
        });
      } else {
        yield put({
          type: 'errorMsg',
          payload: {
            code: response.code,
            msg: response.msg,
          },
        });
      }
    },
  },

  reducers: {
    errorMsg(state, action) {
      return {
        ...state,
        isError: true,
        ...action.payload,
      };
    },
    getCustomerOrder(state, action) {
      const newData = action.payload;
      let newDataSource = [];
      if (newData.isFirstPage) {
        newDataSource = newData.list;
      } else {
        newDataSource = state.ordersList.concat(newData.list);
      }
      return {
        ...state,
        ordersList: newDataSource,
        hasNextPage: newData.hasNextPage,
        nextPage: newData.nextPage,
      };
    },
    getSearchOrder(state, action) {
      let searchedData = [];
      if (action.payload.code === 0) {
        if (action.payload.data.list.length !== 0) {
          searchedData = [...action.payload.data.list];
        } else {
          Toast.fail('查无此订单!!!', 2);
        }
      }
      return {
        ...state,
        searchData: searchedData,
      };
    },
    cancelSearchOrder(state) {
      const searchedData = [];
      return {
        ...state,
        searchData: searchedData,
      };
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname.includes('myorders')) {
          dispatch({
            type: 'getListOrder',
            payload: {
              customerId: 1,
              pageNum: 0,
            },
          });
        }
      });
    },
  },
};
